package com.project.controller;

import com.project.bean.UserBean;
import com.project.service.IUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService service;

    @RequestMapping("login")
    public String login(String userName,String pwd){
        //得到主体
        Subject subject = SecurityUtils.getSubject();
        try {
            //登陆完成后，shiro会将登陆用户绑定在session中
            subject.login(new UsernamePasswordToken(userName, pwd));
            //得到登陆用户对象
            UserBean user = (UserBean)subject.getPrincipal();
        }catch (Exception e){
            return "no";
        }
        return "ok";
    }

    @RequestMapping("add")
    public String add(UserBean user){
        service.add(user);
        return "ok";
    }
}
